//
//	(c) 2009 Andrew Kalmacky
//

#ifndef _AK_STRING_OUTPUT_STREAM_H_
#define _AK_STRING_OUTPUT_STREAM_H_

#include "ltm_string.h"

namespace ak
{
    namespace string
    {
        class string_output_stream: public std::vector<char_t>
        {
        public:
            template<typename T>
            string_output_stream& operator<< (const T& t)
            {
                size_t dst = size();
                resize(dst + chunk<T>::size(t));
                chunk<T>::render(&*begin() + dst, t);
                return *this;
            }
            template<typename T>
            string_output_stream& operator, (const T& t)
            {
                return *this << t;
            }
        };
        template<>
        struct chunk<string_output_stream>: chunk< std::vector<char_t> >
        {
        };
    }
}

namespace Log
{
    inline Stream &operator<< (Stream& s, const ak::string::string_output_stream& v)
    {
        if (v.size())
        {
            if (*(v.end()-1) == 0)
                s << *v.begin();
            else
            {
                ak::string::string_output_stream& vv = const_cast<ak::string::string_output_stream&>(v);
                vv.push_back(0);
                s << &*vv.begin();
                vv.pop_back();
            }
        }
        return s;
    }
}
/*#ifdef TESTS
namespace ak
{
    namespace string
    {

        struct string_output_stream_test
        {
            string_output_stream_test()
            {
                ltm::copy_solver solver;

                string_output_stream s;
                s << T_("asdf"), T_('q'), -10, size_t(12);
                string_output_stream r;
                r << s, string(12), s;
            }
        } string_output_stream_test_initiator;

    }
}
#endif*/

#endif // _AK_STRING_OUTPUT_STREAM_H_
